using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace TrovaIntruso.core
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TextView : Microsoft.Xna.Framework.DrawableGameComponent
    {

        public bool AutoScroll { get; set; }
        public bool CanScroll { get; set; }

        private string _text="";
        public string Text {
            get { return Text; }
            set {
                _text = StaticOperation.WrapText(value, surface.Width - 20);
                drawingStrings= loadStrings(_text);
            }
        }

        public float StartPosition { get; set; }

        protected float lineHeight;

        public string[] drawingStrings;

        public int[] alpha;

        protected float scrollVelocity = 1.73f;

        public Rectangle surface=new Rectangle();

        public TextView(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        private string[] loadStrings(string s) {
            string[] strs = s.Split('\n');
            alpha = new int[strs.Length];
            for (int i = 0; i < alpha.Length; i++)
            {
                alpha[i] = 255;
            }
            if (s.Length > 0)
            {
                lineHeight = StaticOperation.DefaultFont.LineSpacing;
            }
            return strs;
        }

        public void setSurface(Rectangle rect) {
            surface = rect;
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            
            if(AutoScroll)
                StartPosition -= scrollVelocity;
            else if (CanScroll)
            {
                if (StaticOperation.GestureReady )
                    if(StaticOperation.LastGesture.GestureType == Microsoft.Xna.Framework.Input.Touch.GestureType.VerticalDrag)
                        StartPosition += StaticOperation.LastGesture.Delta.Y;
            }
            checkLine();
            base.Update(gameTime);
        }

        private void checkLine() {
            if(drawingStrings!=null)
                for (int i = 0; i < drawingStrings.Length; i++)
                {
                    float position = lineHeight * i + StartPosition;
                    float perc = 25;
                    float temp = 0; ;

                    if (position <= surface.Y + perc || position > surface.Y + surface.Height - perc)
                    {
                        alpha[i] = 0;
                    }
                    else if (position <= surface.Y + surface.Height - perc || position < surface.Y + perc)
                    {
                        alpha[i] = 255;
                    }
                    
                }
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch s = StaticOperation.Batch;
            s.Begin();
            Vector2 pos=new Vector2(surface.X,surface.Y+StartPosition);
            Color a=Color.Black;
            if (drawingStrings != null)
                for (int i = 0; i < drawingStrings.Length; i++)
                {
                    if (alpha[i] != 0) {
                        a.A=(byte)alpha[i];
                        s.DrawString(StaticOperation.DefaultFont, drawingStrings[i], pos, a);
                    
                    }
                    pos.Y += lineHeight;
                }
            s.End();
            base.Draw(gameTime);
        }
    }
}
